//
//  MeetingViewController.swift
//  jiahui
//
//  Created by 3tee on 2024/11/27.
//

import UIKit

import RxCocoa
import RxSwift
import Popover

class MeetingViewController: UIViewController {

    
    lazy var bag = DisposeBag()
    @IBOutlet weak var toolStackView: UIStackView!
    @IBOutlet weak var topToolView: UIView!
    @IBOutlet weak var leaveBtn: UIButton!
    @IBOutlet weak var speakerBtn: UIButton!
    @IBOutlet weak var switchCarmeraBtn: UIButton!
    //bottomTool
    @IBOutlet weak var audioBtn: UIButton!
    @IBOutlet weak var videoBtn: UIButton!
    @IBOutlet weak var sharedBtn: UIButton!
    @IBOutlet weak var userBtn: UIButton!
    @IBOutlet weak var moreBtn: UIButton!
    
    var popover: Popover?
    var subViewControllers = [UIViewController]()
    
    lazy var morePopView: MorePopView = {
        $0.frame = CGRect(x: 0, y: 0, width: 54, height: 140)
        $0.friendCricleBtn.addTarget(self, action: #selector(goFriendCricle), for: .touchUpInside)
        $0.secondBtn.rx.tap.subscribe {[weak self] _ in
            guard let self = self else {return}
            self.navigationController?.pushViewController(BluetoothVC(), animated: true)
            self.popover?.dismiss()
        }.disposed(by: bag)
        return $0
    }(MorePopView())
    
    override func viewWillAppear(_ animated: Bool) {
        navigationItem.hidesBackButton = true
        navigationController?.navigationBar.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        navigationItem.hidesBackButton = false
        navigationController?.navigationBar.isHidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
     
        
       leaveBtn.rx.tap.subscribe { [weak self] evenet in
            ThreeTManager.shardInstance.leaveRoom()
            self!.navigationController?.popViewController(animated: true)
        }.disposed(by: bag)
        
        switchCarmeraBtn.rx.tap.subscribe {[weak self] event in
            ThreeTManager.shardInstance.switchCramera()
        }.disposed(by: bag)
        
        speakerBtn.rx.tap.subscribe {[weak self] event in
            ThreeTManager.shardInstance.speakerMode(1)
        }.disposed(by: bag)
        
        audioBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.audioBtn.isSelected = !strongSelf.audioBtn.isSelected
            print("点击了音频按钮")
            ThreeTManager.shardInstance.openMic(isOpen: strongSelf.audioBtn.isSelected)
        }.disposed(by: bag)
        
        videoBtn.rx.tap.subscribe {[weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.videoBtn.isSelected = !strongSelf.videoBtn.isSelected
            ThreeTManager.shardInstance.openCamera(strongSelf.videoBtn.isSelected)
            print("点击了视频按钮")
            
        }.disposed(by: bag)
        
        sharedBtn.rx.tap.subscribe {[weak self] _ in
            print("点击了共享按钮")
        }.disposed(by: bag)
        
        userBtn.rx.tap.subscribe {[weak self] _ in
            print("点击了参会者按钮")
        }.disposed(by: bag)
        
        moreBtn.rx.tap.subscribe {[weak self] _ in
            print("点击了更多按钮")
            guard let self = self else{return}
            self.popover?.show(self.morePopView, fromView: self.moreBtn)
        }.disposed(by: bag)
       
    }
    
    
    func setupUI() {
        audioBtn.isSelected = UserDefaultsPlugin.bool(forKey:Constant.key_audio)
        videoBtn.isSelected = UserDefaultsPlugin.bool(forKey: Constant.key_video)
        let pageviewcontroller = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal)
         subViewControllers = [DriveViewController(),LecturerViewController(),GalleryViewController()]
        //pageviewcontroller.view.backgroundColor = .systemGray6
        pageviewcontroller.delegate = self
        pageviewcontroller.dataSource = self
        pageviewcontroller.view.frame = CGRectMake(0, navigationBar_height, self.view.frame.width, self.view.frame.height - 66 - navigationBar_height)
        self.addChild(pageviewcontroller)
        self.view.addSubview(pageviewcontroller.view)
        pageviewcontroller.setViewControllers([subViewControllers[1]], direction: .forward, animated: true)

        // Do any additional setup after loading the view.
        view.bringSubviewToFront(topToolView)
        let popOpts: [PopoverOption] = [
            .type(.up),
            .blackOverlayColor(.clear)
        ]
        popover = Popover(options: popOpts)
        popover?.popoverColor = .systemGray6
        
    }
    
    

    func indexOfViewController(_ name: UIViewController) -> Int? {
       return subViewControllers.firstIndex(of: name)
    }
     
    func viewcontrollerOfIdx(_ idx: Int) -> UIViewController? {
        return subViewControllers[idx]
    }
    
    @objc func goFriendCricle(){
        self.present(FriendCricleVC(), animated: true)
    }
    
}

extension MeetingViewController{
    
   
    
    
    
    
}



extension MeetingViewController: UIPageViewControllerDataSource{
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        var idx = indexOfViewController(viewController)
        print("前一个vc index = \(idx) vcs = \(pageViewController.viewControllers)")
        if idx == 0 {
            //toolStackView.isHidden = true;
            return nil
        }else{
            idx! -= 1
           
            return viewcontrollerOfIdx(idx!) ?? nil
        }
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        var idx = indexOfViewController(viewController)
        print("后一个vc index = \(idx) vcs = \(pageViewController.viewControllers)")

        //toolStackView.isHidden = !(idx! > 0)
        if idx == subViewControllers.count - 1 {
            return nil
        }else{
            idx! += 1
           

            return viewcontrollerOfIdx(idx!) ?? nil
        }
    }
    
    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return subViewControllers.count
    }
    
    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return 1
    }
}

extension MeetingViewController: UIPageViewControllerDelegate{
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed {
            print(" 滑didFinishAnimating 结束 vcs = \(pageViewController.viewControllers)")
            if let vc = pageViewController.viewControllers?.first as? DriveViewController {
                toolStackView.isHidden = true
            }else{
                toolStackView.isHidden = false
            }
        }

    }
}

extension MeetingViewController{
    
}
